﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace System
{
	public partial class fDialogCoords : Form
	{
		public fDialogCoords(String Caption)
		{
			InitializeComponent();
			Text = Caption;
			L_Value.AutoSize = true;
		}

		public bool SetCoord(ref V3G Old)
		{
			if (Old.Valid)
			{
				E_Coord.Text =
				Old.LongDeg.ToString("000") + Old.LongMin.ToString("00") + Old.LongSec.ToString("00.0000") + (Old.Longitude < 0 ? "W" : "E")
				+ " " +
				Old.LatDeg.ToString("00") + Old.LatMin.ToString("00") + Old.LatSec.ToString("00.0000") + (Old.Longitude < 0 ? "S" : "N");
			}
			if (ShowDialog() == DialogResult.OK)
			{
				Old = new V3G(E_Coord.Text);
				return true;
			}
			else
				return false;
		}

		private void btnSet_Click(object sender, EventArgs e)
		{
			try
			{
				V3G Res = new V3G(E_Coord.Text);
				DialogResult = DialogResult.OK;
			}
			catch
			{
				MessageBox.Show("Wrong format!");
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}

		private void E_Latitude_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				btnSet_Click(null, null);
			}
		}

		private void E_Longitude_TextChanged(object sender, EventArgs e)
		{
			try
			{
				L_Value.Text = new V3G(E_Coord.Text).ToString().Replace(" ", "\n");
				L_Value.ForeColor = Color.Green;
			}
			catch
			{
				L_Value.Text = "Invalid!";
				L_Value.ForeColor = Color.Red;
			}
		}
	}
}